import javax.swing.*;
import java.awt.*;

class RectangleGUI {
    public static void showRectangleWindow() {
        JFrame frame = new JFrame("矩形计算器");
        frame.setSize(500, 400);
        frame.setLayout(new BorderLayout());

        // 输入面板
        JPanel inputPanel = new JPanel(new GridLayout(3, 2, 10, 10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel lengthLabel = new JLabel("长度:");
        JTextField lengthField = new JTextField();
        JLabel widthLabel = new JLabel("宽度:");
        JTextField widthField = new JTextField();
        JButton calculateBtn = new JButton("计算");
        JButton drawBtn = new JButton("绘制图形");

        inputPanel.add(lengthLabel);
        inputPanel.add(lengthField);
        inputPanel.add(widthLabel);
        inputPanel.add(widthField);
        inputPanel.add(calculateBtn);
        inputPanel.add(drawBtn);

        // 结果面板
        JTextArea resultArea = new JTextArea();
        resultArea.setEditable(false);

        calculateBtn.addActionListener(e -> {
            try {
                double length = Double.parseDouble(lengthField.getText());
                double width = Double.parseDouble(widthField.getText());
                double area = length * width;
                double perimeter = 2 * (length + width);

                String result = String.format("矩形计算结果:\n长度: %.2f\n宽度: %.2f\n面积: %.2f\n周长: %.2f",
                        length, width, area, perimeter);
                resultArea.setText(result);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "请输入有效的数字！");
            }
        });

        drawBtn.addActionListener(e -> {
            try {
                double length = Double.parseDouble(lengthField.getText());
                double width = Double.parseDouble(widthField.getText());
                // 打开单独的绘图窗口
                new RectangleDrawingWindow(length, width);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "请输入有效的数字！");
            }
        });

        frame.add(inputPanel, BorderLayout.NORTH);
        frame.add(new JScrollPane(resultArea), BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class RectangleDrawingWindow extends JFrame {
    private double length;
    private double width;

    public RectangleDrawingWindow(double length, double width) {
        this.length = length;
        this.width = width;
        initializeUI();
    }

    private void initializeUI() {
        setTitle("矩形图形 - 长度: " + length + " 宽度: " + width);
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        RectangleDrawingPanel drawingPanel = new RectangleDrawingPanel(length, width);
        add(drawingPanel);

        setVisible(true);
    }
}

class RectangleDrawingPanel extends JPanel {
    private double length;
    private double width;

    public RectangleDrawingPanel(double length, double width) {
        this.length = length;
        this.width = width;
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 启用抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (length > 0 && width > 0) {
            g2d.setColor(Color.ORANGE);
            g2d.setStroke(new BasicStroke(3));

            int centerX = getWidth() / 2;
            int centerY = getHeight() / 2;

            // 缩放矩形以便显示
            int scaledLength = (int) (length * 8);
            int scaledWidth = (int) (width * 8);

            Rectangle rect = new Rectangle(centerX - scaledLength / 2, centerY - scaledWidth / 2, scaledLength, scaledWidth);
            g2d.draw(rect);

            // 标注尺寸
            g2d.setColor(Color.RED);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 14));
            g2d.drawString("长度: " + length, centerX, centerY - scaledWidth / 2 - 15);
            g2d.drawString("宽度: " + width, centerX + scaledLength / 2 + 10, centerY);
        }
    }
}